package com.crm.system.service.impl.customerManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.customerManager.CustomerReturn;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.customerManager.CustomerReturnQO;
import com.crm.model.vo.customerManager.CustomerReturnVO;
import com.crm.model.vo.followPlanManager.FollowPlanContactListVO;
import com.crm.service.customerManager.CustomerReturnDbService;
import com.crm.system.service.customerManager.CustomerReturnService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/22 16:27
 */
@Service
public class CustomerReturnServiceImpl implements CustomerReturnService {
    @Autowired
    CustomerReturnDbService customerReturnDbService;

    @Override
    public Boolean insertCustomerReturn(String accountId, String customerId, String returnType, String returnReason) {
        CustomerReturn customerReturn = new CustomerReturn();
        customerReturn.setCustomerReturnId(CommonUtils.getGUID());//主键
        customerReturn.setReturnPeopleId(accountId);//退回人id
        customerReturn.setReturnTm(new Date());//退回时间
        customerReturn.setReturnReason(returnReason);//退回原因
        customerReturn.setReturnType(returnType);//退回类型 1：管理端收回；2：用户端释放
        customerReturn.setCustomerId(customerId);//客户ID
        return customerReturnDbService.insertCustomerReturn(customerReturn) > 0;
    }

    /**
     * 系统收回客户记录
     * @param pageIndex
     * @param pageSize
     * @param moveCustomerQO
     * @param sortName
     * @param sortOrder
     * @return
     */
    @Override
    public DhPageInfo<CustomerReturnVO> getSysCustomerReturnList(Integer pageIndex, Integer pageSize, CustomerReturnQO moveCustomerQO, String sortName, String sortOrder) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if(!StringUtils.isBlank(sortName)&&!StringUtils.isBlank(sortOrder)){
            switch(sortName){
                case "returnTm":
                {
                    order.append("return_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        moveCustomerQO.setDateType(getDateUnit(moveCustomerQO.getDateType()).toString());
        List<CustomerReturnVO> list =customerReturnDbService.getFollowPlanListByDate(moveCustomerQO,order);
        PageInfo<CustomerReturnVO> pageInfo=new PageInfo<>(list);
        return new DhPageInfo<CustomerReturnVO>(pageInfo,list);
    }

    /**
     * 日期类型(全部/本周/本月/本年度)
     * @Author chaifuyou
     * @Date  2021/4/1 16:12
     * @param dateType -(all:全部; thisWeek:本周; thisMonth:本月; thisYear:本年；thisToday:今天)
     * @return {@link {java.lang.Integer}}
     */
    private Integer getDateUnit(String dateType) {
        Integer dateUnit = 0;
        if (!StringUtils.isBlank(dateType)) {
            if ("thisWeek".equals(dateType)) {
                //本周
                dateUnit = 1;
            }
            if ("thisMonth".equals(dateType)) {
                //本月
                dateUnit = 2;
            }
            if ("thisYear".equals(dateType)) {
                //本年
                dateUnit = 3;
            }
            if ("thisToday".equals(dateType)) {
                //今天
                dateUnit = 4;
            }
        }
        return dateUnit;
    }
}
